<template>
  <div id="propPieChart"></div>
</template>
<script setup>
import * as echarts from 'echarts'
import { getDifferentConut, getDifferentConutByPro } from '@/api/activeFault'
import imgUrl from '@/assets/images/activeFault/age-little.png'

const route = useRoute()
const { provinceName } = route.query
const createEcharts = (color) => {
  let myChart = echarts.init(document.getElementById("propPieChart"));
  myChart && myChart.setOption({
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '0',
      textStyle: {
        color: "#fff",
        fontSize: 14,
        padding: [0, 4]
      },
      itemWidth: 10,
      itemHeight: 10
    },
    color: color,
    graphic: {
      elements: [
        {
          type: 'image',
          style: {
            image: imgUrl,
            width: 60,
            height: 60,
          },
          left: '19%',
          top: 'center'
        },
        {
          type: 'image',
          style: {
            image: imgUrl,
            width: 60,
            height: 60,
          },
          left: '69%',
          top: 'center'
        }
      ]
    },
    title: [
      {
        text: '全国占比',
        left: '10%',
        textAlign: 'left',
        textStyle: {
          color: '#fff',
          fontSize: 16
        }
      },
      {
        text: provinceName + '占比',
        left: '60%',
        textAlign: 'left',
        textStyle: {
          color: '#fff',
          fontSize: 16
        }
      }
    ],
    series: [
      {
        type: 'pie',
        radius: ['35%', '55%'],
        center: ['25%', '50%'],
        labelLine: {
          show: false
        },
        data: data.value,
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#000947',
          borderWidth: 6
        },
        label: {
          show: true,
          color: '#46FFFF',
          formatter: '{c}%',
          fontSize: 12
        },
        labelLine: {
          length: 0,
          length2: 0
        }
      },
      {
        type: 'pie',
        radius: ['35%', '55%'],
        center: ['75%', '50%'],
        labelLine: {
          show: false
        },
        data: provinceData.value,
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#000947',
          borderWidth: 6
        },
        label: {
          show: true,
          color: '#46FFFF',
          formatter: '{c}%',
          fontSize: 12
        },
        labelLine: {
          length: 0,
          length2: 0
        }
      }
    ]
  })
}

let color = ['#EF862B', '#46FFFF', '#A6D839', '#4555F0']
const type = [
  { value: '0', name: '全新世' },
  { value: '1', name: '晚更新世' },
  { value: '2', name: '早中更新世' },
  { value: '3', name: '前第四纪' }
]
const data = ref([])
const provinceData = ref([])
const differentConut = async () => {
  let res = await getDifferentConut()
  data.value = res.data.map(item => ({
    name: type.find(item1 => item.activityEraType == item1.value)?.name,
    value: item.activityEraProportion
  }))
  let result = await getDifferentConutByPro({ province: provinceName })
  provinceData.value = result.data.map(item => ({
    name: type.find(item1 => item.activityEraType == item1.value)?.name,
    value: item.activityEraProportion
  }))
  createEcharts(color)
}


onMounted(async () => {
  differentConut()
  window.addEventListener('resize', resize)
})
const resize = () => {
  let myChart = echarts.init(document.getElementById("numPieChart"));
  myChart.resize()
}
onBeforeUnmount(() => {
  window.removeEventListener('resize', resize)
})
</script>
<style scoped lang="scss">
#propPieChart {
  width: 450px;
  font-size: 14px;
  // background: url('@/assets/images/activeFault/age-bg.png') no-repeat;
  // background-position: 15% 100%;
  // background-size: 55% 100%;
  margin: 10px 0;
  height: calc(100% - 70px);
  min-height: 125px;
  // min-height: 300px;

  &>div {
    height: 100%;
  }
}
</style>